<script setup>

import {deleteById, tree} from "@/apis/sys/dept.js";
import {reactive, ref, withDirectives} from "vue";
import {OPERATE_DRAWER_MODE} from "@/common/constant.js";
import {NButton, NSpace, useDialog, useMessage} from "naive-ui";
import permission from "@/directive/permission.js";
import OperateDrawer from "@/views/sys/dept/operate-drawer.vue";

onMounted(() => {
  loadTableData()
})
const message = useMessage()
const dialog = useDialog()
const loading = ref(false)
const tableData = ref([])
const loadTableData = async () => {
  loading.value = true
  const {code, data} = await tree()
  loading.value = false
  if (code === 0) {
    tableData.value = data
  }
}
const operateDrawerOption = reactive({
  visible: false,
  mode: OPERATE_DRAWER_MODE.DETAIL,
  id: undefined
})
const showAddDrawer = () => {
  operateDrawerOption.mode = OPERATE_DRAWER_MODE.ADD
  operateDrawerOption.visible = true
}
const showModifyDrawer = (id) => {
  operateDrawerOption.mode = OPERATE_DRAWER_MODE.EDIT
  operateDrawerOption.id = id
  operateDrawerOption.visible = true
}
const showDetailDrawer = (id) => {
  operateDrawerOption.mode = OPERATE_DRAWER_MODE.DETAIL
  operateDrawerOption.id = id
  operateDrawerOption.visible = true
}
const columns = ref([
  {
    title: '部门名称',
    key: 'name',
  },
  {
    title: '部门编号',
    key: 'deptNo'
  },
  {
    title: '创建时间',
    align: 'center',
    key: 'createdAt',
  },
  {
    title: '最后更新时间',
    align: 'center',
    key: 'updatedAt',
  },
  {
    title: '操作',
    align: 'center',
    key: 'operate',
    fixed: 'right',
    width: 220,
    render: (row) => {
      return h(NSpace, {justify: 'center'}, () => {
        return [
          withDirectives(h(NButton, {onClick: () => showModifyDrawer(row.id),}, () => '编辑'), [[permission, 'sys:user:modify']]),
          withDirectives(h(NButton, {onClick: () => showDetailDrawer(row.id),}, () => '详情'), [[permission, 'sys:user:detail']]),
          withDirectives(h(NButton, {onClick: () => onDelete(row.id),}, () => '删除'), [[permission, 'sys:user:delete']]),
        ]
      })
    }
  }
])
const onDelete = async (id) => {
  dialog.warning({
    title: '删除',
    content: '确定删除吗？',
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: async () => {
      const {code} = await deleteById(id)
      if (code === 0) {
        message.success("删除成功")
        await loadTableData()
      }
    }
  })
}
</script>

<template>
  <n-space vertical>
    <n-button type="primary" @click="showAddDrawer">新增</n-button>
    <n-data-table
        :scroll-x="1200"
        remote
        ref="table"
        :columns="columns"
        :data="tableData"
        :loading="loading"
        :row-key="(rowData) => rowData.id"
    />
  </n-space>
  <operate-drawer v-model:show="operateDrawerOption.visible"
                  :parent-data="tableData"
                  :mode="operateDrawerOption.mode"
                  :id="operateDrawerOption.id"
                  @success="loadTableData"
                  :menuParentData="tableData"/>
</template>

<style scoped>

</style>
